package com.agent.service.impl;

import com.agent.entity.CourseSchedule;
import com.agent.mapper.CourseScheduleMapper;
import com.agent.service.CourseScheduleService;
import com.agent.utils.ExcelParserUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleMapper, CourseSchedule> implements CourseScheduleService {

    private final CourseScheduleMapper courseScheduleMapper;

    // 导入课程表
    public List<CourseSchedule> importCourseSchedule(MultipartFile file) {
        try {

            ExcelParserUtils excelParserUtils = new ExcelParserUtils();
            List<CourseSchedule> courseSchedules = excelParserUtils.parseExcel(file);
            // 可选：批量入库
            if (courseSchedules != null && !courseSchedules.isEmpty()) {
                saveCourseSchedulesBatch(courseSchedules);
            }
            return courseSchedules;
        } catch (Exception e) {
            System.err.println("导入课程表失败: " + e.getMessage());
            return List.of();
        }
    }

    // 插入课程信息
    public void saveCourseSchedulesBatch(List<CourseSchedule> courses) {
        if (courses != null && !courses.isEmpty()) {
            // 使用自定义批量插入
            int i = courseScheduleMapper.insertBatchSomeColumn(courses);
            if (i > 0){
                System.out.println("批量插入成功！");
            } else {
                throw new RuntimeException("批量插入失败！");
            }
        }
    }

    // 分页查询所有课程信息
    public List<CourseSchedule> list() {
        return super.list();
    }

    // 根据id查询课程信息
    public CourseSchedule getById(Long id) {
        return super.getById(id);
    }

    // 添加课程信息
    public boolean save(CourseSchedule courseSchedule) {
        return super.save(courseSchedule);
    }

    // 删除课程信息
    public boolean removeById(Long id) {
        return super.removeById(id);
    }

    // 修改课程信息
    public boolean updateById(CourseSchedule courseSchedule) {
        return super.updateById(courseSchedule);
    }
}